package com.bsoft.gol.editor.utils;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class HttpPostUploadUtil {
    @Value("${image_url}/imageServlet")
    private String uploadUrl;
    @Value("$${image_url}")
    private String netServiceUrl;
    public HttpPostUploadUtil() {
    }

    public String getUploadUrl() {
        return this.uploadUrl;
    }

    public void setUploadUrl(String uploadUrl) {
        this.uploadUrl = uploadUrl;
    }

    public String getNetServiceUrl() {
        return this.netServiceUrl;
    }

    public void setNetServiceUrl(String netServiceUrl) {
        this.netServiceUrl = netServiceUrl;
    }

    public String formUpload(MultipartFile file, String... sizes) {
        String urlStr = this.uploadUrl;
        String res = "";
        HttpURLConnection conn = null;
        String BOUNDARY = "---------------------------123821742118716";

        try {
            JSONObject obj;
            if (null != sizes && sizes.length > 0) {
                JSONObject textObj = new JSONObject();
                JSONArray arr = new JSONArray();

                for(int i = 0; i < sizes.length; ++i) {
                    String[] sis = sizes[i].split("x");
                    if (null != sis && sis.length == 2) {
                        obj = new JSONObject();
                        obj.put("w", sis[0]);
                        obj.put("h", sis[1]);
                        arr.add(obj);
                    }
                }

                textObj.put("sizes", arr);
                urlStr = urlStr + "?imageinfo=" + URLEncoder.encode(textObj.toJSONString(), "utf-8");
            }

            URL url = new URL(urlStr);
            conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(30000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            StringBuffer strBuf;
            if (file != null) {
                String contentType = "image/jpeg";
                strBuf = new StringBuffer();
                strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                strBuf.append("Content-Disposition: form-data; name=\"" + file.getName() + "\"; filename=\"" + file.getName() + "\"\r\n");
                strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
                out.write(strBuf.toString().getBytes());
                DataInputStream in = new DataInputStream(file.getInputStream());
               // int bytes = false;
                byte[] bufferOut = new byte[1024];

                int bytes;
                while((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }

                in.close();
            }

            byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
            out.write(endData);
            out.flush();
            out.close();
            strBuf = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;

            while((line = reader.readLine()) != null) {
                strBuf.append(line).append("\n");
            }

            res = strBuf.toString();
            reader.close();
            obj = null;
        } catch (Exception var17) {
            System.out.println("发送POST请求出错。" + urlStr);
            var17.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }

        }

        return res;
    }
}
